<template>
  <view class="container">
    <uni-forms ref="form">
      <uni-form-item label="用户名" name="">
        <uni-easyinput v-model="user.name" @change="usernameChange" placeholder="请输入姓名" />
      </uni-form-item>
      <uni-form-item label="密码" name="">
        <uni-easyinput v-model="user.password" placeholder="请输入密码" type="password" />
      </uni-form-item>
    </uni-forms>
    <uni-button type="primary" @click="login">登录</uni-button>
    <uni-button type="primary" @click="register" style="margin-top: 20px;">注册</uni-button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      user: {
        id: 0,
        name: "",
        password: "",
        email: "",
        telephone: "",
        department: "",
        major: ""
      }
    };
  },
  methods: {
    usernameChange(e) {
      console.log(e.detail.value); // 获取输入的值
    },
    login() {
      const data = this.user;
      uni.request({
        url: 'http://localhost:8090/freshmenapp/user/login',
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: (res) => {
          if (res.data.success) {
            if (res.data.data) {
              uni.setStorageSync('name', res.data.data.name);
              uni.setStorageSync('isLogin', true);
            }
            uni.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 2000
            })
			uni.switchTab({
			  url: '/pages/my/my'
			})
			console.log('准备跳转'); // 添加调试信息
          } else {
            uni.showToast({
              title: '登录失败: ' + (res.data.message || '未知错误'),
              icon: 'none',
              duration: 2000
            });
          }
        },
        fail: (err) => {
          uni.showToast({
            title: '请求失败',
            icon: 'none',
            duration: 2000
          });
        }
      });
    },
    register() {
      uni.navigateTo({
        url: '/pages/my/register'
      })
    }
  },
  onShow() {
    uni.onTabBarMidButtonTap(function (e) {
      console.log(e);
    });
  }
};
</script>

<style>
.container {
  padding: 20px;
  font-size: 14px;
  line-height: 24px;
}
</style>